package dproxy.server.net.backoff;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Generates an exponential backoff.
 */
class PowerNIntervalGenerator extends IntervalGenerator {

    /** Expoent. */
    private static final int N = 3;
    
    /**
     * @see IntervalGenerator#getIntervals(int, int)
     */
    @Override
    public Collection<Integer> getIntervals(int readTimeout, int readRetestTimes) {
        Collection<Integer> times = new ArrayList<Integer>(readRetestTimes);
        
        double normalizer = (double) readTimeout / Math.pow(readRetestTimes, N);
        double oldValue = 0;
        for (int i = 0; i < readRetestTimes + 1; i++) {
            double currentValue = Math.pow(i, N);
            times.add((int) (normalizer * (currentValue-oldValue)));
            oldValue = currentValue;
        }
        
        return times;
    }

}
